package com.coder.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.rental.entity.CarInfo;
import com.coder.rental.entity.Maintain;
import com.coder.rental.service.ICarInfoService;
import com.coder.rental.service.IMaintainService;
import com.coder.rental.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 车辆保养 前端控制器
 * </p>
 *
 * @author zhaojj
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/rental/maintain")
public class MaintainController {
    @Resource
    private IMaintainService maintainService;

    @Resource
    private ICarInfoService carInfoService;
    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start, @PathVariable int size,
                         @RequestBody Maintain maintain){
        Page<Maintain> page = new Page<>(start,size);
        return Result.success(maintainService.searchByPage(page,maintain));
    }
    @PostMapping
    public Result save(@RequestBody Maintain maintain){
        CarInfo carInfo=carInfoService.selectByCarNum(maintain.getAutoNum());
        maintain.setAutoId(carInfo.getId());
        // 将车辆实保次数+1
        carInfo.setActualNum(carInfo.getActualNum()+1);
        carInfoService.updateById(carInfo);
        return maintainService.save(maintain)? Result.success(): Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody Maintain maintain){
        Maintain oldMaintain=maintainService.getById(maintain.getId());
        if(!oldMaintain.getAutoNum().equals(maintain.getAutoNum())){
            return Result.fail().setMessage("车牌号码不允许修改");
        }
        return maintainService.updateById(maintain)? Result.success(): Result.fail();
    }

    @DeleteMapping("{ids}")
    public Result delete(@PathVariable String ids){
        return maintainService.delete(ids)? Result.success() : Result.fail();
    }
}
